package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.page.PageList;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)/*查询事务*/
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;
    @Override
    public List queryAll() {
        return mapper.loadAll();
    }
    @Override
    public T queryOne(Long id) {
        return mapper.loadOne(id);
    }
    @Override
    @Transactional
    public void add(T t) {
        mapper.add(t);
    }
    @Override
    @Transactional
    public void delete(Long id) {
        mapper.delete(id);
    }
    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }
    @Override
    public PageList queryPage(BaseQuery baseQuery) {
        /*1.查询总条数
         * 2.查询当前页数据
         * */
        /*查询总条数*/
        Integer total = mapper.loadTotal(baseQuery);

        /*查询当前页的数据*/
        List<T> list = mapper.loadData(baseQuery);

        return new PageList<T>(total,list);
    }

    @Override
    @Transactional
    public void batchDelete(List ids) {
        mapper.batchDelete(ids);
    }
}
